/*
 * FileName: MessageUtil.java
 * Author:   zzw
 * Date:     2019年01月25日
 * Description:
 */
package com.chezhibao.flink.util;

import org.apache.flink.api.java.tuple.Tuple13;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 〈〉<br>
 * 〈〉
 *
 * @author zzw
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本]（可选）
 */
public class MessageUtil {

    private static Logger logger = LoggerFactory.getLogger(MessageUtil.class);

    //traceId ─traceTime-startTime─rpcType─appName─rpcId─traceName─serviceName─methodName─remoteIp─span0─span1─resultCode─requestSize─responseSize─ext
    public static void convertMap(Object[] objects, Tuple13<String, String, String, String, String, String, String, String, String, String, String, String, String> record,Integer rpcType){
        String traceId = String.class.cast(record.getField(0));
        Long traceTime = Long.parseLong(traceId.substring(8,21));
        long startTime = Long.valueOf(record.getField(1));
        String appName = record.getField(3);
        String rpcId = record.getField(4);
        String traceName = "";
        String serviceName = "";
        String methodName = "";
        String remoteIp = "";
        String resultCode = "";
        int requestSize = 0;
        int responseSize = 0;
        int span0 = 0;
        int span1 = 0;
        String ext = "";
        try {
            if (rpcType == 0){
                traceName = record.getField(6);//traceName
                span1 = Integer.parseInt(record.getField(5));//span1
                resultCode = record.getField(7);//resultCode
                if (record.getField(8) != null){
                    ext = record.getField(8);
                }
            }
            else if (rpcType == 1 || rpcType == 3 || rpcType == 4 || rpcType == 5) {
                serviceName = record.getField(5);
                methodName = record.getField(6);
                remoteIp = record.getField(7);
                String[] spans = String.class.cast(record.getField(8)).split(",");
                span0 = Integer.valueOf(spans[0].substring(1));
                span1 = Integer.valueOf(spans[1].substring(0,spans[1].length() - 1).trim());
                resultCode = record.getField(9);
                if(record.getField(10) != null){
                    requestSize = Integer.parseInt(record.getField(10));
                }
                if(record.getField(11) != null){
                    responseSize = Integer.parseInt(record.getField(11));
                }
                if (record.getField(12)  != null) {
                    ext = record.getField(12);
                }
            }
            else if (rpcType == 2) {
                serviceName = record.getField(5);
                methodName = record.getField(6);
                remoteIp = record.getField(7);
                span1 = Integer.valueOf(String.class.cast(record.getField(8)));
                resultCode = record.getField(9);
                if(record.getField(10) != null){
                    requestSize = Integer.parseInt(record.getField(10));
                }
                if(record.getField(11) != null){
                    responseSize = Integer.parseInt(record.getField(11));
                }
                if (record.getField(12)  != null) {
                    ext = record.getField(12);
                }
            }
            else if (rpcType == 6) {
                serviceName = record.getField(5);
                methodName = record.getField(6);
                remoteIp = record.getField(7);
                if ("MQSend".equals(serviceName)) {
                    String span = String.class.cast(record.getField(8));
                    String[] spans = span.substring(1, span.length() - 1).split(",");
                    span0 = Integer.valueOf(spans[0].trim());
                    span1 = Integer.valueOf(spans[1].trim());
                } else {
                    span1 = Integer.valueOf(String.class.cast(record.getField(8)));
                }
                resultCode = record.getField(9);
                if(record.getField(10) != null){
                    requestSize = Integer.parseInt(record.getField(10));
                }
                if(record.getField(11) != null){
                    responseSize = Integer.parseInt(record.getField(11));
                }
                if (record.getField(12)  != null) {
                    ext = record.getField(12);
                }
            }
            else if (rpcType == 9) {
                return;
            }
            else {
                printConvertErrorInfo(record);
                return;
            }
        } catch (Exception e) {
            logger.error("convert error",e);
            printConvertErrorInfo(record);
        }
        //traceId ─traceTime-startTime─rpcType─appName─rpcId─traceName─serviceName─methodName─remoteIp─span0─span1─resultCode─requestSize─responseSize─ext
        objects[0] = traceId;
        objects[1] = traceTime;
        objects[2] = startTime;//startTime
        objects[3] = rpcType;//rpcType
        objects[4] = appName;//appName
        objects[5] = rpcId;//rpcId
        objects[6] = traceName;
        objects[7] = serviceName;
        objects[8] = methodName;
        objects[9] = remoteIp;
        objects[10] = span0;
        objects[11] = span1;
        objects[12] = resultCode;
        objects[13] = requestSize;
        objects[14] = responseSize;
        objects[15] = ext;
    }

    private static void printConvertErrorInfo(Tuple13<String, String, String, String, String, String, String, String, String, String, String, String, String> record){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append((String)record.getField(0)).append("|")
                .append((String)record.getField(1)).append("|")
                .append((String)record.getField(2)).append("|")
                .append((String)record.getField(3)).append("|")
                .append((String)record.getField(4)).append("|")
                .append((String)record.getField(5)).append("|")
                .append((String)record.getField(6)).append("|")
                .append((String)record.getField(7)).append("|")
                .append((String)record.getField(8)).append("|")
                .append((String)record.getField(9)).append("|")
                .append((String)record.getField(10)).append("|")
                .append((String)record.getField(11)).append("|")
                .append((String)record.getField(12)).append("|");
        logger.info("meesage" + stringBuilder.toString());
    }
}
